<?php
// 单独使用时，初始化本文件中的类涉及到的常量
defined('PAGE_ROW_COUNT') || define('PAGE_ROW_COUNT', 20);

/**
 * 一个简单的分页类.
 *
 * @author netmou <leiyanfo@sina.com>
 */
class Page
{
  private $rowCount; //每页显示的记录条数
  private $offset; //当前页记录开始位置
  private $total; //记录总条数
  private $curPage; //当前被选中的页
  private $pageTotal; //分页总数
  private $pageLink; //每个分页的链接

  /**
   * @param offset   每页显示的条目数
   * @param total   总条目数
   * @param curPage   当前被选中的页
   * @param pageLink  每个分页的链接
   */
  public function __construct($total, $queryData = null, $rowCount = null, $page = 'page')
  {
    $this->rowCount = ($rowCount == null) ? PAGE_ROW_COUNT : intval($rowCount);
    $this->total = intval($total);
    $this->pageTotal = max(1, ceil($total / $this->rowCount));
    $this->curPage = min(max(1, intval($_GET[$page])), $this->pageTotal);
    $this->offset = ($this->curPage - 1) * $this->rowCount;
    if(!is_array($queryData)){
      parse_str($queryData, $parseRst);
      $queryData = $parseRst;
    }
    // http_build_query 默认以进行url转码
    $queryStr = http_build_query($queryData);
    if ($queryStr != '') {
      $this->pageLink = "?{$queryStr}&{$page}=";
    } else {
      $this->pageLink = "?{$page}=";
    }
    // 特殊html实体字符进行转码
    $f = array('&','\'','"','<','>');
    $t = array('&amp;','&#039;','&quot;','&lt;','&gt;');
    $this->pageLink = str_replace($f, $t, $this->pageLink);
  }

  /**
   * @return int 分页开始的位置
   */
  public function getRowCount()
  {
    return $this->rowCount;
  }
  /**
   * @return int 分页偏移量
   */
  public function getOffset()
  {
    return $this->offset;
  }
  /**
   * @return string 补充上limit子句
   */
  public function getLimitSQL($no_limit_sql = ''){
    return $no_limit_sql ." LIMIT {$this->rowCount} OFFSET {$this->offset}";
  }
  /**
   * @param $list   每次显示的页数
   * @param $jumpMax   跳转框每次向前，向后显示页码数 (乘2)
   */
  public function showLinks($list = 6, $jumpMax = 50)
  {
    $pageFragment = null;
    
    $from = max(floor($this->curPage - $list / 2), 1);
    $to = min(ceil($this->curPage + $list / 2), $this->pageTotal);
    $pageFragment .= "<span class=\"page page-total\">共 {$this->total} 条</span>";
    $pageFragment .= "<span class=\"page page-count\">每页 {$this->rowCount} 条</span>";
    $pageFragment .= "<span class=\"page page-process\">第 {$this->curPage}/{$this->pageTotal} 页</span>";
    if ($from > 1) {
      $homePage = $this->pageLink.'1';
      $pageFragment .= "<a href=\"{$homePage}\" class=\"page page-first lnk\">首页</a>";
    }
    if ($this->curPage > 1) {
      $prevPage = $this->pageLink . ($this->curPage - 1);
      $pageFragment .= "<a  href=\"{$prevPage}\" class=\"page page-prev lnk\">上一页</a>";
    }
    if ($this->pageTotal > 1) {
      for ($i = $from; $i < $this->curPage; ++$i) {
        $pageAddr = $this->pageLink . $i;
        $pageFragment .= "<a href=\"{$pageAddr}\" class=\"page page-digit lnk\">{$i}</a>";
      }
      $pageFragment .= "<span class=\"page page-digit-cur\">{$this->curPage}</span>";
      for ($i = $this->curPage + 1; $i <= $to; ++$i) {
        $pageAddr = $this->pageLink . $i;
        $pageFragment .= "<a href=\"{$pageAddr}\" class=\"page page-digit lnk\">{$i}</a>";
      }
    }

    if ($this->curPage < $this->pageTotal) {
      $nextPage = $this->pageLink . ($this->curPage + 1);
      $pageFragment .= "<a  href=\"{$nextPage}\" class=\"page page-next lnk\">下一页</a>";
    }
    if ($to < $this->pageTotal) {
      $rearPage = $this->pageLink.$this->pageTotal;
      $pageFragment .= "<a href=\"{$rearPage}\" class=\"page page-last lnk\">尾页</a>";
    }
    if ($this->pageTotal > $list) {
      $pageFragment .= '<label class="page page-jump-label">跳转到 <select class="page-jump" style="line-height:1;padding:0 2px" onchange="window.location=\'' . $this->pageLink . '\'+this.value">';
      $jumpStart = max($from - $jumpMax, 1);
      for ($i = $jumpStart;$i <= $from - 1;++$i) {
        if ($this->curPage == $i) {
          $pageFragment .= "<option selected value=\"{$i}\">$i</option>";
          continue;
        }
        $pageFragment .= "<option value=\"{$i}\">$i</option>";
      }
      $pageFragment .= '<option selected disabled>...</option>';
      $jumpEnd = min($jumpMax + $to, $this->pageTotal);
      for ($i = $to + 1;$i <= $jumpEnd;++$i) {
        if ($this->curPage == $i) {
          $pageFragment .= "<option selected value=\"{$i}\">$i</option>";
          continue;
        }
        $pageFragment .= "<option value=\"{$i}\">{$i}</option>";
      }

      $pageFragment .= '</select></label>';
    }

    return $pageFragment;
  }
  /**
   * 获取指定页码的链接地址
   *
   * @param integer $p 页码
   * @return string 页面链接
   */
  public function getPageLink($p = 1){
    return $this->pageLink . min($this->pageTotal, max($p, 1));
  }
}
// use example:
// $input = $func->inputData();
// $sql = "select * from `xxx` where 1=1 {$condition}";
// $total = $pdo->resultCount($sql);
// $page = new Page($total, $input);
// $sql .= " order by xxx desc limit {$page->getOffset()}, {$page->getRowCount()}";
// $stmt = $pdo->query($sql);
// while($info = $stmt->fetch()):
//   // do ...
// endwhile;
// $page->showLinks();